%header
  %h1= "史萊姆大戰"

%article
  = link_to '開始新遊戲', {:controller => "home", :action => "new_game"}, :id => "new_game", :remote => true, "data-type" => :json
  %br
  %br
  = link_to '刷新', "#", :id => "reload"
  = link_to '結束回合', "#", :id => "end_turn"
  %a{:id => "show_log", :href => '#'} 敵方步驟

  #foobar
  
  #battle

  %a{:id => "bear_pawn", :href => '#'} 生小史萊姆
  %a{:id => "bear_knight", :href => '#'} 生騎士史萊姆
  %a{:id => "bear_archer", :href => '#'} 生弓箭史萊姆
  / %a{:id => "kill", :href => '#'}  殺
  
  :javascript
    var bindFunctions = function(battle) {
      $("#reload").click(function(){
        battle.ajaxUpdate();
      })
      $("#end_turn").click(function(){
        battle.endTurn();
      })
      $("#bear_pawn").click(function(){
        battle.toBear("pawn");
      })
      $("#bear_knight").click(function(){
        battle.toBear("knight");
      })
      $("#bear_archer").click(function(){
        battle.toBear("archer");
      })
      $("#show_log").click(function(){
        battle.showLogStep();
      })
      $("#kill").click(function(){
        battle.setKill();
      })
    };

    var main = function() {
      var battle = new slimeWar.Battle(36, 8);
      var board  = new slimeWar.Board(battle, "battle", 1500);
      var game   = new slimeWar.Game(board, 30);
      game.init();
      bindFunctions(battle);

      var imgPaths = {
        my:    {"defualt":"/images/slimes/slime_my_default.png", "mother":"/images/slimes/slime_my_big.png",
                "knight" :"/images/slimes/slime_my_knight.png" , "archer":"/images/slimes/slime_my_arrow.png"},
        enemy: {"defualt":"/images/slimes/slime_en_default.png", "mother":"/images/slimes/slime_en_big.png",
                "knight" :"/images/slimes/slime_en_knight.png" , "archer":"/images/slimes/slime_en_arrow.png"}};
      game.initImages(imgPaths);

      game.mainLoop();
    };

    $(function() {
      $('#new_game').bind("ajax:success", function(event, data) {
        main()
      });
    });
    

  /
    :javascript
      var updateSlimes = function(mySlimes, enemySlimes) {
        slimeWar.battle.update(mySlimes, enemySlimes);
        slimeWar.board.drawSlimes(mySlimes, 0);
        slimeWar.board.drawSlimes(enemySlimes, 1);
       // document.write("/images/slimes/slime");
      }
          
    
    :javascript
      var mySlimes, enemySlimes
      $(function() {
        $('#update_foobar').bind("ajax:success", function(event, data) {
          mySlimes = JSON.parse(data.my);
          enemySlimes = JSON.parse(data.enemy);
          updateSlimes(mySlimes, enemySlimes);
        });
      });
      $(function() {
        $('#go').bind("ajax:success", function(event, data) {
          mySlimes = JSON.parse(data.my);
          enemySlimes = JSON.parse(data.enemy);
          updateSlimes(mySlimes, enemySlimes);
        });
      });
    
    
    
